const path = require("path");
const { app, protocol, BrowserWindow, globalShortcut, Notification, Menu, Tray } = require("electron");

// 注册协议方案
protocol.registerSchemesAsPrivileged([{ scheme: "app", privileges: { secure: true, standard: true } }]);
// 消除chrome的跨域警告
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = true;
// 消除chrome的连接不安全警告
app.commandLine.appendSwitch("--ignore-certificate-errors", "true");

// 设置开机隐藏式自动启动
app.setLoginItemSettings({
  openAtLogin: true,
  openAsHidden: true, // mac
  args: ["--openAsHidden"], // win
});

let mainWindow = null;
let systemTray = null;
let quitFlag = false;

// 初始化
const init = () => {
  // 创建应用窗口
  mainWindow = new BrowserWindow({
    title: "叶花弈的网卡工具",
    iconType: "custom",
    icon: path.join(__dirname, "icon.ico"),
    width: 568,
    height: 600,
    fullscreen: false,
    fullscreenable: false,
    show: true, // 窗口是否在创建时显示
    frame: true, // 窗口是否有边框
    center: true, // 窗口是否在屏幕居中
    resizable: false, // 窗口是否支持尺寸调整
    maximizable: false,
    transparent: true, // 窗口透明
    backgroundColor: "#00000000",
    // 网页功能设置
    webPreferences: {
      webSecurity: false, // 跨域
      nodeIntegration: true, // 节点继承
      nodeIntegrationInWorker: true,
      preload: path.join(__dirname, "preload.cjs"), // 预加载脚本
    },
  });
  mainWindow.setMenu(null);
  switch (app.isPackaged) {
    // 通过应用程序启动
    case true:
      mainWindow.loadFile(path.resolve(__dirname, "../dist/index.html"));
      break;
    // 通过vscode本地启动
    case false:
      mainWindow.loadURL("http://localhost:8080");
      break;
  }
  // 创建系统托盘
  systemTray = new Tray(path.join(__dirname, process.platform == "darwin" ? "icon.icns" : "icon.ico"));
  systemTray.setToolTip("叶花弈的网卡工具");
  systemTray.setContextMenu(
    Menu.buildFromTemplate([
      // 版本号
      {
        label: "版本号: 3.0.0",
        click: () => sendMessage("叶花弈的网卡工具", "当前已是最新版本: 3.0.0"),
      },
      // 打开
      {
        label: "打开",
        click: () => mainWindow.show(),
      },
      // 退出
      {
        label: "退出",
        click: () => {
          quitFlag = true;
          app.quit();
        },
      },
    ])
  );
  systemTray.on("click", () => handleShowHide("show"));
  // systemTray.on("double-click", () => handleShowHide("show"));
  // 键盘快捷键
  globalShortcut.register("CommandOrControl+Shift+i", () => {
    mainWindow.webContents.openDevTools();
  });
  globalShortcut.register("CommandOrControl+Space", () => {
    if (mainWindow.isVisible()) {
      handleShowHide("hide");
    } else {
      handleShowHide("show");
    }
  });
  // 阻止应用直接关闭
  mainWindow.on("close", (event) => {
    if (!quitFlag) {
      event.preventDefault();
      handleShowHide("hide");
    }
  });
};
// 发送桌面端消息
const sendMessage = (title, body) => {
  const message = new Notification({
    icon: path.join(__dirname, "icon.ico"),
    title,
    body,
  });
  message.show();
};
// 控制桌面端展示||隐藏
const handleShowHide = (type) => {
  switch (type) {
    case "show":
      mainWindow.show();
      break;
    case "hide":
      if (process.platform == "darwin") {
        app.hide();
      } else {
        mainWindow.hide();
      }
      break;
  }
};

app.on("ready", () => {
  init();
  sendMessage("叶花弈的网卡工具", "快捷唤醒: ctrl+space");
});
app.on("will-quit", () => {
  // 注销快捷键
  globalShortcut.unregister("CommandOrControl+Shift+i");
  globalShortcut.unregister("CommandOrControl+Space");
  // 释放资源
  systemTray.destroy();
});
